// pages/product/goods/goodsList/goodsList.js
Page({

  /**糕点
   * 页面的初始数据
   */
  data: {
    baseUrl: 'http://140.143.18.217:8080',
    loadImageUrl: '/product/img/loadImg/',
    pageLengthUrl: '/product/goods/getPageLengthByParam',
    goodsListUrl: '/product/goods/findByCondition/',
    typeListUrl: '/product/dict/findByType',
    searchValue: '',
    typeBar: [{ id: 0, codeValue: '最热商品' }],
    index: 0, //当前激活的类型
    pageSize: 1,
    pageNow: 1,
    goodsList: [],
    searchType: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var params = this.getParams();
    //0.加载页数
    this.getPageCount(params);
    //1.加载类型
    this.getTypeList();
    //2.加载类型对应的商品
    this.getGoodsList(params, this.data.pageNow);
  },

  searchInput: function (e) {
    this.setData({
      searchValue: e.detail.value
    })
  },
  searchStart: function (e) {
    this.setData({ searchType: 0 });
    this.loadData();
  },
  selectByType: function (e) {
    //设置下标
    var clickIndex = e.currentTarget.dataset.index;
    this.setData({
      index: clickIndex,
      searchValue: '',
      searchType: 1
    });
    console.log('selectByType:' + clickIndex );
    this.loadData();
  },
  toDetail: function (e) {
    //设置下标
    var clickIndex = e.currentTarget.dataset.index;
    console.log('toDetail:' + clickIndex);
    wx.setStorageSync('goodsId', this.data.goodsList[clickIndex].id);
    wx.navigateTo({
      url: '/pages/product/goods/goodsDetail/goodsDetail',
    })
  },
  //上拉加载
  bindDownLoad: function () {
    console.log('bindDownLoad');
    if (this.data.pageNow < this.data.pageSize) {
      //页号+1
      this.setData({
        pageNow: this.data.pageNow + 1
      });
      var params = this.getParams();
      //请求加载商品
      this.getGoodsList(params, this.data.pageNow);
    } else {
      wx.showToast({ title: '加载完毕' });
    }
  },
  //下拉刷新
  refresh: function (event) {
    console.log('refresh');
    this.loadData();
  },
  //加载商品
  loadData: function () {
    var params = this.getParams();
    this.data.goodsList = [];
    this.setData({
      pageNow: 1
    });
    //0.加载页数
    this.getPageCount(params);
    //2.加载类型对应的商品
    this.getGoodsList(params, this.data.pageNow);
  },
  //获取商品类型
  getTypeList: function () {
    var sendUrl = this.data.baseUrl + this.data.typeListUrl;
    var that = this;
    wx.request({
      url: sendUrl,
      data: 'GoodsType',
      method: 'post',
      success: function (res) {
        wx.showToast({ title: '请求成功' });
        // 将新获取的数据 res.data.list，concat到前台显示的showlist中即可。

        that.setData({
          typeBar: that.data.typeBar.concat(res.data.data)
        })
        console.log('success getTypeList:' + that.data.typeBar.length);
      },
      fail: function (res) {
        wx.showToast({ title: res });
      }
    });
  },
  getGoodsList: function (params, pageNo) {
    var sendUrl = this.data.baseUrl + this.data.goodsListUrl + pageNo;
    var that = this;
    wx.request({
      url: sendUrl,
      data: params,
      method: 'post',
      success: function (res) {
        wx.showToast({ title: '加载成功' });
        // 将新获取的数据 res.data.list，concat到前台显示的showlist中即可。
        console.log('success getGoodsList:' + res.data.data.length);
        that.setData({
          goodsList: that.data.goodsList.concat(res.data.data)
        })
      },
      fail: function (res) {
        wx.showToast({ title: res });
      }
    });
  },
  getPageCount: function (params) {
    //data.data.data[0]
    var sendUrl = this.data.baseUrl + this.data.pageLengthUrl;
    var that = this;
    wx.request({
      url: sendUrl,
      data: params,
      method: 'post',
      success: function (res) {
        wx.showToast({ title: '加载成功' });
        // 将新获取的数据 res.data.list，concat到前台显示的showlist中即可。
        console.log('success getPageCount:' + res.data.data[0]);
        that.setData({
          pageSize: res.data.data[0]
        })
      },
      fail: function (res) {
        wx.showToast({ title: res });
      }
    });
  },
  //拼装接口请求
  getParams: function () {
    var params = new Array(); 
    if (this.data.searchType == 0) { //通过输入框搜索
      this.setData({ index: 0 });
      params.push({ fieldName: 'name', compare: 'like', value: this.data.searchValue });
      console.log(params);
    } else if (this.data.index > 0) { //按照类型搜索 
      params.push({
        fieldName: 'typeId', compare: 'idIn', value: this.data.typeBar[this.data.index].id, 'childClassName': 'com.victualler.product.goods.entity.GoodsTypeDetailDao'
      });
    }
    params.push({ fieldName: 'state', compare: '=', value: '0' });
    return params;
  }
})